﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace HungTrung
{
    public class GiaCam
    {
        private PictureBox _picturebox;
       // Timer timemove;
        public DateTime timebatdau = System.DateTime.Now;
        private int tocdoroi;
        private int _solanroi;
        bool flagdichuyentrai;

        public int Solanroi
        {
            get { return _solanroi; }
            set { _solanroi = value; }
        }
        
        public static List<GiaCam> ListGiaCam= new List<GiaCam>();
        public int Top
        {
            set { _picturebox.Top = value; }
            get { return _picturebox.Top; }
        }

        public int Left
        {
            set { _picturebox.Left = value; }
            get { return _picturebox.Left; }
        }
        public GiaCam(char loai)
        {
            Solanroi = 1;
            tocdoroi = 3;
            flagdichuyentrai = true;// di chuyen qua trai
            _picturebox = new PictureBox();
            _picturebox.Size = new Size(95, 76);
            _picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
            _picturebox.BackColor = Color.Transparent;
            switch (loai)
            {
                case 'g': _picturebox.Image = Properties.Resources.ga;
                    break;

                case 'v': _picturebox.Image = Properties.Resources.vit; 
                    break;

                case 'n': _picturebox.Image = Properties.Resources.ngan;
                    break;
            }


            //timemove = new Timer();
          //  timemove.Interval = 5;
           // timemove.Tick += new EventHandler(updatvitri);

            ListGiaCam.Add(this);
        }
        public void thatrung( Form form)
           {
            Trung _trung = new Trung();
            _trung.Left = this.Left+32;
            _trung.Top = this.Top + 95;
            _trung.Speed = this.tocdoroi;

            //random trung hoac chat thai
            if (random() == 1)// tao chat thai
            {
                _trung.taochatthai();
            }

            _trung.Add(form);
            // kiem tra so lan roi trung + cap nhat toc do roi
            if (this.Solanroi == 20)
            {
                this.tocdoroi += 3;
                this.Solanroi = 0;
            }
         //   timemove.Start();
        }

        public void AddtoForm(Form form)
        { 
            form.Controls.Add(_picturebox); 
        }
        public void xoa()
        {
            if (_picturebox.Parent != null)
            {
                _picturebox.Parent.Controls.Remove(_picturebox);
            }
        }

        public int random()// random tao chat thai hoac trung
        { 
             Random _randomvalue= new Random();
             return _randomvalue.Next(2);
        }

        public void updatvitri()
        {

            if (flagdichuyentrai == true)// di chuyen qua ben trai
            {
                dichuyentrai();
                kiemtravuotbien();
            }
            else
            {
                dichuyenphai();
                kiemtravuotbien();
            }
        }
        private void kiemtravuotbien()
        {
            if (this.Left < 20)
            {
                flagdichuyentrai = false;
                return;
            }
            if (this.Left > 610)
            {
                flagdichuyentrai = true;
                    return;
            }
        }
        private void dichuyentrai()
        {
            this.Left -= 10;
            
        }
        private void dichuyenphai()
        {
            this.Left += 10;
        }
        
    }
}
